package com.eshop.modules.system.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import com.eshop.modules.system.service.dto.DictDetailDto;
import java.util.Collection;
import cn.hutool.core.collection.CollectionUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.system.service.dto.DictDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.system.service.dto.DictQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.system.service.DictService;
import com.eshop.modules.system.domain.Dict;
import com.eshop.modules.system.service.mapper.DictMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class DictServiceImpl extends BaseServiceImpl<DictMapper, Dict> implements DictService
{
    private final IGenerator generator;
    
    public Map<String, Object> queryAll(final DictQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<Dict> page = (PageInfo<Dict>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)DictDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<Dict> queryAll(final DictQueryCriteria criteria) {
        return (List<Dict>)((DictMapper)this.baseMapper).selectList(QueryHelpPlus.getPredicate(Dict.class, criteria));
    }
    
    public void download(final List<DictDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final DictDto dict : all) {
            if (CollectionUtil.isNotEmpty((Collection)dict.getDictDetails())) {
                for (final DictDetailDto dictDetail : dict.getDictDetails()) {
                    final Map<String, Object> map = new LinkedHashMap<String, Object>();
                    map.put("字典名称", dict.getName());
                    map.put("字典描述", dict.getRemark());
                    map.put("字典标签", dictDetail.getLabel());
                    map.put("字典值", dictDetail.getValue());
                    map.put("创建日期", dictDetail.getCreateTime());
                    list.add(map);
                }
            }
            else {
                final Map<String, Object> map2 = new LinkedHashMap<String, Object>();
                map2.put("字典名称", dict.getName());
                map2.put("字典描述", dict.getRemark());
                map2.put("字典标签", null);
                map2.put("字典值", null);
                map2.put("创建日期", dict.getCreateTime());
                list.add(map2);
            }
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public DictServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
